package com.qf.controller.servlet;

import cn.dsna.util.images.ValidateCode;
import com.qf.domain.Address;
import com.qf.domain.User;
import com.qf.service.AddressService;
import com.qf.service.UserService;
import com.qf.service.impl.AddressServiceImpl;
import com.qf.service.impl.UserServiceImpl;
import com.qf.utils.Base64Utils;
import com.qf.utils.StringUtils;
import jdk.nashorn.internal.ir.RuntimeNode;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author YDW
 * projectName:shopping
 * time:2022/4/18 14:59
 * description:
 */
@WebServlet("/userservlet")
public class UserServlet extends BaseServlet{
    UserService userService=new UserServiceImpl();
    AddressService addressService=new AddressServiceImpl();


    public void checkUserName(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取username
        String username = request.getParameter("username");
        //非空验证
        if(StringUtils.isEmpty(username)){
            response.getWriter().print("2");
            return;
        }
        //发起校验

        //若用户名存在，返回true，不存在返回false
        boolean flag=userService.checkUsername(username);
        if(flag){
            response.getWriter().print("1");
        }else {
            response.getWriter().print("0");
        }
    }

    public String register(HttpServletRequest request,HttpServletResponse response){

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String repassword = request.getParameter("repassword");
        String gender = request.getParameter("gender");
        String email = request.getParameter("email");
        //非空验证
        if(StringUtils.isEmpty(username)||StringUtils.isEmpty(password)||StringUtils.isEmpty(email)){
            return "redirect:/register.jsp";
        }
        //验证密码是否一致
        if(!password.equals(repassword)){
            return "redirect:/register.jsp";
        }
        //调用业务逻辑
        //判断用用户是否存在
        boolean flag=userService.checkUsername(username);
        if(flag){
            return "redirect:/register.jsp";
        }
        //注册用户
        boolean issuccess=userService.register(username,password,email,gender);
        if(issuccess){//注册成功跳转成功页面
            return "redirect:/registerSuccess.jsp";
        }else {//失败跳转注册页面
            return "redirect:/register.jsp";
        }
    }

    public void code(HttpServletRequest request,HttpServletResponse response) throws IOException {
        //创建验证码
        ValidateCode validateCode=new ValidateCode(100,40,4,15);
        //保存验证码
        request.getSession().setAttribute("code",validateCode.getCode());
        //输出验证码
        validateCode.write(response.getOutputStream());
    }
    public void checkCode(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String inputcode = request.getParameter("code");
        String code =(String) request.getSession().getAttribute("code");
        if(StringUtils.isEmpty(inputcode)){
            response.getWriter().print("1");
            return;
        }
        if(inputcode.equalsIgnoreCase(code)){
            response.getWriter().print("0");
        }else {
            response.getWriter().print("1");
        }

    }
    public String login(HttpServletRequest request,HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String auto = request.getParameter("auto");
        if(StringUtils.isEmpty(username)||StringUtils.isEmpty(password)){
            return "redirect:/login.jsp";
        }
        User user=userService.login(username,password);
        if(user!=null){
            if("on".equalsIgnoreCase(auto)){
                Cookie cookie=new Cookie("userinfo",Base64Utils.encode(username+"&"+password));
                cookie.setMaxAge(60*60*24*7*2);
                cookie.setPath("/");
                response.addCookie(cookie);
            }
            request.getSession().setAttribute("user",user);
            return "redirect:/index.jsp";
        }else {
            return "redirect:/login.jsp";
        }
    }
    public String logOut(HttpServletRequest request,HttpServletResponse response){
        //清除session
        request.getSession().removeAttribute("user");
        //清除cookie
        Cookie cookie=new Cookie("userinfo","");
        cookie.setMaxAge(1);
        cookie.setPath("/");
        response.addCookie(cookie);
        return "redirect:/login.jsp";
    }
    //获取地址列表界面
    public String  getAddress(HttpServletRequest request,HttpServletResponse response) throws IOException {
        User user=(User)request.getSession().getAttribute("user");
        if (user == null) {
            response.sendRedirect("userservlet?method=login");
        }
        List<Address> addList = addressService.findAddListByUid(user.getId());


        request.setAttribute("addList",addList);
        return "forward:/self_info.jsp";
    }
    public String  addAddress(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String detail = request.getParameter("detail");
        if(StringUtils.isEmpty(name)||StringUtils.isEmpty(phone)||StringUtils.isEmpty(detail)){
            return "redirect:/userservlet?method=getAddress";
        }
        User user=(User) request.getSession().getAttribute("user");
        if (user == null) {
            response.sendRedirect("userservlet?method=login");
        }
        Address address=new Address();
        address.setDetail(detail);
        address.setLevel(0);
        address.setName(name);
        address.setPhone(phone);
        address.setUid(user.getId());
        addressService.insert(address);
        return "redirect:/userservlet?method=getAddress";
    }
    public String deleteAddress(HttpServletRequest request,HttpServletResponse response) throws IOException {
        User user=(User) request.getSession().getAttribute("user");
        if (user == null) {
            response.sendRedirect("userservlet?method=login");
        }
        String Aid = request.getParameter("id");
        addressService.deleteAddressByAid(Aid);
        return "redirect:/userservlet?method=getAddress";
    }
    public String updateAddress(HttpServletRequest request,HttpServletResponse response) throws IOException {
        User user=(User) request.getSession().getAttribute("user");
        if (user == null) {
            response.sendRedirect("userservlet?method=login");
        }
        String name = request.getParameter("name");
        String phone = request.getParameter("phone");
        String detail = request.getParameter("detail");
        String id = request.getParameter("id");
        String level = request.getParameter("level");
        if(StringUtils.isEmpty(name)||StringUtils.isEmpty(phone)||StringUtils.isEmpty(detail)){
            return "redirect:/userservlet?method=getAddress";
        }
        Address address=new Address();
        address.setDetail(detail);
        address.setLevel(Integer.parseInt(level));
        address.setName(name);
        address.setPhone(phone);
        address.setUid(user.getId());
        address.setId(Integer.parseInt(id));
        addressService.update(address);
        return "redirect:/userservlet?method=getAddress";
    }
    //设置默认收货地址
    public String defaultAddress(HttpServletRequest request,HttpServletResponse response) throws IOException {
        User user=(User) request.getSession().getAttribute("user");
        if (user == null) {
            response.sendRedirect("userservlet?method=login");
        }
        String Aid = request.getParameter("id");
        addressService.defauletAddress(Aid,user.getId());
        return "redirect:/userservlet?method=getAddress";
    }

}
